/**
 * Created by Brant on 2016/12/28.
 */
module.exports = function ($http, httpservice, $q) {
    var customerBarService = {};


    /*查询自然人列表的接口*/
    customerBarService.selectZiRanRenInfo = function (params) {
      return $http({
            method: 'GET',
            url: httpservice.selectZRRCustomerUrl,
            params: params,
            dataType: 'json',
            headers: {'Content-Type': 'application/json'}

        });
    };
    /*查询机构列表的接口*/
    customerBarService.selectFaRenInfo = function (params) {
        return $http({
            method: 'GET',
            url: httpservice.selectFRCustomerUrl,
            params: params,
            dataType: 'json',
            headers: {'Content-Type': 'application/json'}
        });
    };
    //获取自然人客户左侧列表
    customerBarService.getPersonListGroup = function () {
      return $http({
        method: 'GET',
        url: httpservice.customerGetPersonList
      })
    };
    //获取机构客户左侧列表
    customerBarService.getGroupCompanyList = function () {
      return $http({
        method: 'GET',
        url: httpservice.customerGetCompanyList
      })
    };
    //自然人搜索
    customerBarService.searchPersonCustomer = function (params) {
      return $http({
        method: 'get',
        url: httpservice.searchPersonCustomer,
        params:params
      })
    };
    //机构搜索
    customerBarService.searchCustomerCustomer = function (params) {
      return $http({
        method: 'get',
        url: httpservice.searchCustomerCustomer,
        params:params
      })
    };
    //新增左侧分组
    customerBarService.addCustomerGroup = function (params) {
      return $http({
        method: 'post',
        url: httpservice.addCustomerGroup,
        params:params
      })
    };

    //添加客户机构下拉
    customerBarService.findEnterpriseList = function (params) {
      return $http({
        method: 'get',
        url: httpservice.findEnterpriseListUrl,
        params:params
      })
    };


  //添加自然人
    customerBarService.addZRRCustomer = function (params) {
        var deferred = $q.defer();
        $http({
            method: 'POST',
            url: httpservice.addZRRCustomerUrl,
            data:params,
            headers : {'Content-Type':'application/json'}
        }).then(function successCallback(response) {
            deferred.resolve(response.data);
        }, function errorCallback(response) {
            deferred.reject(response);
        });
        return deferred.promise;
    };


    //删除自然人
    customerBarService.deleteZRRCustomer = function (params) {
        // var params = {
        //     "id": id
        // };
        var deferred = $q.defer();
        $http({
            url: httpservice.ZRRDeleteUrl,
            method: 'DELETE',
            data:params,
            headers : {'Content-Type':'application/json'}
        }).then(function successCallback(response) {
            deferred.resolve(response.data);
        }, function errorCallback(response) {
            deferred.reject(response);
        });
        return deferred.promise;
    };

    //添加法人
    customerBarService.addFRCustomer = function (params) {
        return $http(
            {
                url: httpservice.addFRCustomerUrl,
                method: 'POST',
                params: params,
                dataType: 'json',
                headers: {'Content-Type': 'application/json'}
            }
        )
            .then(function (response) {
                return response.data;
            })
            .catch(function (response) {
                console.error('login error', response.status, response.data);
                $("#errormsgId").html("登录失败，失败原因：" + response.status);
            })
            .finally(function () {
                /*$("#errormsgId").html("网络链接失败！");*/
            });
    };


    //删除法人
    customerBarService.deleteFRCustomer = function (params) {
        // var params = {
        //     "id": id
        // };
        var deferred = $q.defer();
        $http({
            url: httpservice.FRDeleteUrl,
            method: 'DELETE',
            data: params,
            dataType: 'json',
            headers: {'Content-Type': 'application/json'}
        }).then(function successCallback(response) {
            deferred.resolve(response.data);
        }, function errorCallback(response) {
            deferred.reject(response);
        });
        return deferred.promise;
    };

    //详情
    //自然人详情
    customerBarService.ZRRDetail = function (id) {
        var params = {"id": id};
        return $http(
            {
                url: httpservice.ZRRDetailUrl,
                method: 'GET',
                params: params,
                dataType: 'json',
                headers: {'Content-Type': 'application/json'}
            }
        )
            .then(function (response) {
                return response.data;
            })
            .catch(function (response) {
                console.error('login error', response.status, response.data);
                $("#errormsgId").html("登录失败，失败原因：" + response.status);
            })
            .finally(function () {
                /*$("#errormsgId").html("网络链接失败！");*/
            });
    };

    //法人详情
    customerBarService.FRDetail = function (params) {
        // var params = {"id": id};
        return $http(
            {
                url: httpservice.FRDetailUrl,
                method: 'GET',
                params: params,
                // dataType: 'json',
                // headers: {'Content-Type': 'application/json'}
            }
        )
            .then(function (response) {
                return response.data;
            })
            .catch(function (response) {
                console.error('login error', response.status, response.data);
                $("#errormsgId").html("登录失败，失败原因：" + response.status);
            })
            .finally(function () {
                /*$("#errormsgId").html("网络链接失败！");*/
            });
    };

    //编辑
    //保存编辑自然人
    customerBarService.ZRREditorUrl = function (id, name, sex, phone, birthday, nationality, profession, workplace,
                                                address, status, remark) {
        var params = {
            "id": id,
            "name": name,
            "sex": sex,
            "contactnumber": phone,
            "birthday": birthday,
            "nationality": nationality,
            "profession": profession,
            "workplace": workplace,
            "address": address,
            "status": status,
            "remark": remark
        };
     /*   return $http(
            {
                url: httpservice.ZRREditorUrl,
                method: 'GET',
                params: params,
                dataType: 'json',
                headers: {'Content-Type': 'application/json'}
            }
        )
            .then(function (response) {
                return response.data;
            })
            .catch(function (response) {
                console.error('login error', response.status, response.data);
                $("#errormsgId").html("登录失败，失败原因：" + response.status);
            })
            .finally(function () {
                /!*$("#errormsgId").html("网络链接失败！");*!/
            });*/
        var deferred = $q.defer();
        $http({
            method: 'PUT',
            url: httpservice.ZRREditorUrl,
            data:params,
            headers : {'Content-Type':'application/json'}
        }).then(function successCallback(response) {
            deferred.resolve(response.data);
        }, function errorCallback(response) {
            deferred.reject(response);
        });
        return deferred.promise;
    };


    //编辑法人
    customerBarService.FREditorUrl = function (params) {
        // var params = {
        //     "id": id,
        //     "companyname": fdname,
        //     "address": fdadd,
        //     "representative": fddb,
        //     "post": fdzw,
        //     "contactname": fdlxr,
        //     // "workplace": workplace,
        //     // "status": status,
        //     "remark": fdbz,
        //     "contactphone": fdphone
        // };
        return $http(
            {
                url: httpservice.FREditorUrl,
                method: 'PUT',
                data: params,
                dataType: 'json',
                headers: {'Content-Type': 'application/json'}
            }
        )
            .then(function (response) {
                return response.data;
            })
            .catch(function (response) {
                console.error('login error', response.status, response.data);
                $("#errormsgId").html("登录失败，失败原因：" + response.status);
            })
            .finally(function () {
                /*$("#errormsgId").html("网络链接失败！");*/
            });
    };

    //案件所属
  customerBarService.caseBelongs = function (params) {
    return $http({
      method:"POST",
      url:httpservice.caseBelongsUrl,
      params:params,
    })
  };

  //编辑自然人
  customerBarService.getPersonDetail = function(params) {
    return $http({
      method:'POST',
      url:httpservice.ZRRDetailUrl,
      params:params,
    })
  };
  //保存编辑自然人
  customerBarService.saveEditPersonIdInfor = function(params) {
    return $http({
      method:'PUT',
      url:httpservice.ZRREditorUrl,
      data:params,
      dataType: 'json',
      headers: {'Content-Type': 'application/json'}
    })
  };
  //编辑分组
  customerBarService.saveEditGroup = function(params) {
    return $http({
      method:'POST',
      url:httpservice.editGroupUrl,
      params:params,
    })
  };
  //删除分组
  customerBarService.deleteEditGroup = function(params) {
    return $http({
      method:'POST',
      url:httpservice.deleteEditGroupUrl,
      params:params,
    })
  };


    return customerBarService;
};
